﻿using System;
using UnityEngine;

// ReSharper disable InvalidXmlDocComment

namespace Core
{
    /// <summary>
    /// 若要使用该类型作为Dict的key，则需要实现IComparable, IComparable<XYWH>,接口
    /// </summary>
    // ReSharper disable once InconsistentNaming
    public struct XYWH : IEquatable<XYWH>
    {
        public static readonly XYWH Invalid = Parse(float.Epsilon, float.Epsilon, float.Epsilon, float.Epsilon);

        public float X;
        public float Y;

        public float Width;
        public float Height;

        public XYWH(float x, float y, float w, float h)
        {
            X = x;
            Y = y;
            Width = w;
            Height = h;
        }

        public static XYWH Parse(float x, float y, float w, float h)
        {
            XYWH obj;
            obj.X = x;
            obj.Y = y;
            obj.Width = w;
            obj.Height = h;
            return obj;
        }

        public static bool operator !=(XYWH lhs, XYWH rhs)
        {
            return !(lhs == rhs);
        }

        public static bool operator ==(XYWH lhs, XYWH rhs)
        {
            return Mathf.Approximately(lhs.X, rhs.X) &&
                   Mathf.Approximately(lhs.Y, rhs.Y) &&
                   Mathf.Approximately(lhs.Width, rhs.Width) &&
                   Mathf.Approximately(lhs.Height, rhs.Height);
        }

        public bool Equals(XYWH other) => this == other;

        public override bool Equals(object obj)
        {
            if (obj == null || (obj is XYWH) == false)
            {
                return false;
            }
            return Equals((XYWH)obj);
        }

        public override int GetHashCode()
        {
            return X.GetHashCode() ^ (Width.GetHashCode() << 2) ^ (Y.GetHashCode() >> 2) ^ (Height.GetHashCode() >> 1);
        }

        public override string ToString()
        {
            return $"({X},{Y},{Width},{Height})";
        }
    }
}

